package ntu.metaweb.update;

import java.io.IOException;
import java.net.URL;

import rice.p2p.commonapi.rawserialization.InputBuffer;
import rice.p2p.commonapi.rawserialization.OutputBuffer;
import ntu.metaweb.XMLMarshaller;
import ntu.p2p.dht.messaging.DHTObjectDifference;

public class UpdateLinkRating implements DHTObjectDifference {
	protected static final short TYPE = 9;
	
	protected URL fromURL;
	protected URL toURL;
	protected String user;
	protected boolean up;
	
	public UpdateLinkRating(URL fromURL, URL toURL, String user, boolean up) {
		this.fromURL = fromURL;
		this.toURL = toURL;
		this.user = user;
		this.up = up;
	}
	
	public UpdateLinkRating(InputBuffer buf) {
		try {
			this.fromURL = (URL) XMLMarshaller.getInstance().unmarshall(buf.readUTF());
			this.toURL = (URL) XMLMarshaller.getInstance().unmarshall(buf.readUTF());
			this.user = buf.readUTF();
			this.up = buf.readBoolean();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static DHTObjectDifference build(InputBuffer buf) {
		return new UpdateLinkRating(buf);
	}
	
	@Override
	public void serialize(OutputBuffer buf) throws IOException {
		buf.writeShort(TYPE);
		buf.writeUTF(XMLMarshaller.getInstance().marshall(fromURL));
		buf.writeUTF(XMLMarshaller.getInstance().marshall(toURL));
		buf.writeUTF(user);
		buf.writeBoolean(up);
	}

}
